package com.liumin.comlink.http.exception;


import android.util.Log;

import androidx.annotation.Nullable;

import com.liumin.publiclib.helper.CrashHandler;

import java.util.Objects;

/**
 * 自定义异常
 * 2019/12/25 10:25
 *
 * @author LiuWeiHao
 */
public class ApiException extends RuntimeException {
    private int code;
    private String message;


    public ApiException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
        postException(new Throwable("code=" + code + message));
    }

    public ApiException(String message, Throwable throwable) {
        super(message, throwable);
        this.message = message;
        postException(new Throwable(message, throwable));
    }

    public ApiException(int code, Throwable throwable) {
        this.code = code;
        postException(new Throwable("code=" + code + message, throwable));
    }

    public ApiException(int code, String message, Throwable throwable) {
        this.code = code;
        this.message = message;
        postException(new Throwable("code=" + code + message, throwable));
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Nullable
    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    private void postException(Throwable throwable) {
        Log.i("postException ", Objects.requireNonNull(throwable.getMessage()));
        throwable.printStackTrace();
        CrashHandler.postCatchedException(throwable);
    }


}


